package be.idamf.mx.rpc
{
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class DynamicResponder implements IResponder
	{
		public var target:Object;
		public var prefix:String;
		
		public function DynamicResponder(target:Object = null, prefix:String = "")
		{
			this.target = target;
			this.prefix = prefix;
		}
		
		public function result(data:Object):void
		{
			var operation:String = prefix + data.token.message.operation;
			
			try
			{
				target[operation + "ResultHandler"](data);
			}
			catch (e:Error)
			{
				trace(operation + "ResultHandler not found on " + target.toString());
			}
		}
		
		public function fault(info:Object):void
		{
			var operation:String = prefix + info.token.message.operation;
			
			try
			{
				target[operation + "FaultHandler"](info);
			}
			catch (e:Error)
			{
				trace(operation + "FaultHandler not found on " + target.toString());
			}
			
			try
			{
				target.faultHandler(info);
			}
			catch (e:Error)
			{
				trace("faultHandler not found on " + target.toString());
			}
		}
	}
}